package cn.touna.jss.modules.monitor.service;

import cn.touna.jss.common.cache.HbaseComponent;
import cn.touna.jss.common.enums.IntfScopeEnum;
import cn.touna.jss.common.factory.ResponseFactory;
import cn.touna.jss.modules.adapter.ApiMapping;
import cn.touna.jss.modules.credit.entity.CreditQueryHistoryEntity;
import cn.touna.jss.modules.credit.entity.CreditQueryHistorySimplifiedEntity;
import cn.touna.jss.modules.credit.service.CreditQueryService;
import cn.touna.risk.api.domain.Response;
import cn.touna.risk.api.enums.SystemErrorCodeEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * \* User: MeiZhongHao
 * \* Date: 2018-06-06
 * \* Time: 18:34
 * \* Description:
 * \
 */
@Service
public class CreditHistoryService {

    @Autowired
    private CreditQueryService creditQueryService;
    @Autowired
    protected HbaseComponent hbaseComponent;

    @ApiMapping(value = "creditHistory.query", recordHistory = false, useCache = false,scope = IntfScopeEnum.JSS_INTF_TEST)
    public Response<List<CreditQueryHistorySimplifiedEntity>> queryCreditHistory(CreditQueryHistoryEntity history) {
        try {
            if (history == null || (StringUtils.isEmpty(history.getMobile()) &&
                    StringUtils.isEmpty(history.getIdNo())))
                return ResponseFactory.fail(SystemErrorCodeEnum.JSS_PARAM_CHECK_FAILED,"idNo/mobile不能为空");
            CreditQueryHistoryEntity entity = new CreditQueryHistoryEntity();
            entity.setIdNo(history.getIdNo());
            entity.setMobile(history.getMobile());
            entity.setResponseCode(history.getResponseCode());
            List<CreditQueryHistorySimplifiedEntity> data = creditQueryService.queryCreditHistorySimplified(entity);
            return ResponseFactory.success(data);
        } catch (Exception e) {
            return ResponseFactory.fail(SystemErrorCodeEnum.JSS_SYSTEM_ERROR, e.getMessage());
        }
    }

    @ApiMapping(value = "hbaseCreditHistory.query", recordHistory = false, useCache = false,scope = IntfScopeEnum.JSS_INTF_TEST)
    public Response<Object> queryCreditHistoryByHBase(CreditQueryHistoryEntity history) {
        try {
            if (history == null || StringUtils.isEmpty(history.getCrNo()))
                return ResponseFactory.fail(SystemErrorCodeEnum.JSS_PARAM_CHECK_FAILED,"crNo不能为空");
            return ResponseFactory.success(hbaseComponent.queryHbaseCreditHistory(history.getCrNo()));
        } catch (Exception e) {
            return ResponseFactory.fail(SystemErrorCodeEnum.JSS_SYSTEM_ERROR, e.getMessage());
        }
    }



}